
/* GCSx
** RUNDATA.H
**
** Runtime data support- stacks, variables, hashes, arrays
*/

/*****************************************************************************
** Copyright (C) 2003-2006 Janson
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
*****************************************************************************/

#include "all.h"

const char memFail[] = "Out of Memory Error";

void createStack(Stack* s) {
    s->data = NULL;
    s->top = NULL;
    s->allocSize = NULL;
}

void destroyStack(Stack* s) {
    free(s->data);
}

void increaseStack(Stack* s) {
    int size;
    int dist;
    if (s->allocSize == NULL) {
        size = 16;
        dist = 0;
    }
    else {
        // @TODO: need to catch runaway recursion at some obscene stack size
        size = (s->allocSize - s->data) << 1;
        dist = s->top - s->data;
    }
    s->data = (StackEntry*)realloc(s->data, size * sizeof(StackEntry));
    if (s->data == NULL) {
        fatalCrash(1, memFail);
    }
    s->top = s->data + dist;
    s->allocSize = s->data + size;
}
